package com.food.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class CheckAccount
 */
@WebServlet("/CheckAccount")
public class CheckAccount extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CheckAccount() {
		super();
		
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		PrintWriter out = response.getWriter();

		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		String url = "jdbc:sqlserver://192.168.34.108:1433;database=PJDB";
		String query = "select count(*) from member where mbaccount=?";
		String mbaccount = request.getParameter("mbaccount");
		try {
			DriverManager
					.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver());
			conn = DriverManager.getConnection(url, "sa", "sa123456");
			stmt = conn.prepareStatement(query);
			stmt.setString(1, mbaccount);

			rs = stmt.executeQuery();
			String strMsg = java.net.URLEncoder.encode("帳號不存在", "UTF-8"); // "帳號不存在";

			rs.next();
			if (rs.getInt(1) >= 1) {
				strMsg = java.net.URLEncoder.encode("帳號已存在", "UTF-8");
			}
			out.print(strMsg);
		} catch (SQLException e) {
			out.println("Error:" + e.getMessage());
		} finally {

		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		// this.doPost(request, response);

	}

}
